package org.example.adminservice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.GoodsMapper;
import org.example.pojo.PageBean;
import org.example.pojo.admindto.GoodsLookDTO;
import org.example.pojo.entity.Goods;
import org.example.adminservice.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void save(Goods goods) {
        if(goods.getName()==null||goods.getName()==""){
            throw new IllegalArgumentException("商品名称不能为空");
        }
        if(goods.getPrice()<=0.0){
            throw new IllegalArgumentException("商品价格不能为0");
        }
        goods.setSalenum(0);
        goodsMapper.insert(goods);
    }

    @Override
    public PageBean checkGoods(GoodsLookDTO goodsLookDTO) {
        Integer page= goodsLookDTO.getPage(),pageSize= goodsLookDTO.getPageSize();
        PageHelper.startPage(page,pageSize);
        List<Goods> list = goodsMapper.adminlist(goodsLookDTO.getName(), goodsLookDTO.getSalenum());
        Page<Goods> goodsPage = (Page<Goods>) list;
        PageBean pageBean = new PageBean(goodsPage.getTotal(),goodsPage.getResult());
        return pageBean;
    }

    @Override
    public void delete(Long id) {
        goodsMapper.deleteById(id);
    }

    @Override
    public void update(Goods goods) {
        goodsMapper.update(goods);
    }
}
